'use strict';

angular.module('app')
    .controller('updateController', ['$scope', '$rootScope', '$state', '$timeout',
        function ($scope, $rootScope, $state, $timeout) {
            var title = "";
            $scope.imgData={};
            var defaultAva = $rootScope.defaultAvatar;
            $scope.myImage = '';
            $scope.record={}
            // $scope.myCroppedImage=$scope.myCroppedImage ;
            $scope.myCroppedImage = '';
            UE.delEditor('container');
            var ue = UE.getEditor('container',{
                serverUrl:"/iFengChao-SYS-Web/upload/uploadUMImage"
            });
            if ($state.includes('**.affiche.update')) {
                title = "编辑资讯";
                var id = $state.params.id;
                activate(id);
                makeList();

            } else if ($state.includes('**.affiche.add')) {
                title = "添加资讯";
                $scope.record.readTimes=0;
                makeList();
            }
            $scope.title = $rootScope.title = title;
            $scope.loading = true;



            $.ajax({
                url : $scope.app.project_name+'/affiche/getLabText',
            }).then(function(data) {
                $scope.labList = data.data;
                $scope.labresults=[];
                for(var i=0;i<$scope.labList.length;i++){
                    var obj={};
                    obj.id=$scope.labList[i].labId;
                    obj.text=$scope.labList[i].labName;
                    $scope.labresults.push(obj);
                }
                console.log($scope.labresults);
                $("#lab").select2({
                    tags: true,
                    data: $scope.labresults,
                    placeholder: "请选择标签"
                });
                $("#lab").val($scope.record.label.split(",")).trigger('change')
            });





            $scope.affsubmit=function() {

                if($scope.record.type==''||$scope.record.type==null){
                    $scope.alertModal({'content':'请选择资讯类别'});
                    return;
                }
                if($scope.record.title==''||$scope.record.title==null){
                    $scope.alertModal({'content':'请输入标题'});
                    return;
                }
                if($scope.record.description==''||$scope.record.description==null){
                    $scope.alertModal({'content':'请输入摘要'});
                    return;
                }
                if($scope.record.keywords==''||$scope.record.keywords==null){
                    $scope.alertModal({'content':'请输入关键字'});
                    return;
                }
                if($scope.record.title.length>60){
                    $scope.alertModal({'content':'标题超出限制'});
                    return;
                }
                if($scope.record.description.length>240){
                    $scope.alertModal({'content':'摘要超出限制'});
                    return;
                }
                var labidstr="";
                console.log($scope.imgData);
                var picUrl = $scope.imgData[0];
                if (!picUrl) {
                    $scope.alertModal({'content':'请上传封面图片'});
                    return;
                }
                var picUrl = $scope.imgData[0].src;
                $scope.record.smallIconUrl = picUrl[0];
                var labdata = $("#lab").select2('data');
                if(labdata.length==0){
                    $scope.alertModal({'content':'请选择标签'});
                    return;
                }
                $scope.record.content = UE.getEditor('container').getContent();
                if($scope.record.content==''){
                    $scope.alertModal({'content':'请选择内容'});
                    return;
                }
                for(var i=0;i<labdata.length;i++){
                    if(i==labdata.length-1){
                        labidstr += labdata[i].id;
                    }else {
                        labidstr += labdata[i].id+",";
                    }
                }
                $scope.record.labid = labidstr;
                $scope.isDisabled = true;//提交disabled
                $scope.record.afficheId = $state.params.id;
                var m = $scope.record;
                $.ajax({
                    url:  $scope.app.project_name + '/affiche/saveAffiche',
                    data: m
                }).then(function (result) {
                    if (result.httpCode == 200) {
                        $state.go('main.web.affiche.list');
                    } else {
                        $scope.alertModal({"content": "操作失败"});
                    }
                });
            };

            // 初始化页面
            function activate(id) {
                $scope.loading = true;
                $.ajax({
                    url: $scope.app.project_name + '/affiche/detail',
                    data: {'id': id}
                }).then(function (result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.$apply(function () {
                            console.log(result.data);
                            $scope.record = result.data;
                            $scope.record.type = result.data.signType;
                            $scope.imgData = [];
                            var srcArr = [];
                            ue.ready(function () {
                                ue.setContent($scope.record.afficheContent);
                            })
                            srcArr.push($scope.record.smallIconUrl);
                            $scope.imgData.push({
                                src:srcArr
                            });
                        });
                    } else {
                        $scope.msg = result.msg;
                    }
                });
            }

            function makeList () {
                $.ajax({
                    url : $scope.app.project_name+'/affiche/getType',
                }).then(function(result) {
                    console.log(result);
                    $scope.$apply(function () {
                        $scope.typeList = result.data;
                    })
                });
            };

        }]);